<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="DUDU的博客">
    <meta name="keyword"  content="博客,blog,技术博客,编程,前端">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        封装一个简易的jquery - DUDU的博客
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 灵感来源于总结，成长来源于分享 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Du Heng</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>
        <div class="index-about-mobile">
            <i> 灵感来源于总结，成长来源于分享 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        封装一个简易的jquery
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2018-06-10 13:04:02</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#前端" title="前端">前端</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#源码" title="源码">源码</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#jquery" title="jquery">jquery</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <p>使用Javascript原生代码尝试封装一个简易的jquery，并实现一个css功能</p>
<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>jQuery 源码分析<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml">    </span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>大家好<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="comment">&lt;!-- &lt;script src="./libs/jquery-1.12.4.js"&gt;&lt;/script&gt; --&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span></span><br><span class="line"><span class="xml">        $('h1').on('click', function () &#123;</span></span><br><span class="line"><span class="xml">            $(this).css('color', 'red');</span></span><br><span class="line"><span class="xml">        &#125;)</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // $() 从语法角度看，必定有一个函数名字为 $</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // jQuery() 从语法角度看，必定有一个函数名字为 jQuery</span></span><br><span class="line"><span class="xml">        // jQuery('h1').on('click', function () &#123;</span></span><br><span class="line"><span class="xml">        //     jQuery(this).css('color', 'red')</span></span><br><span class="line"><span class="xml">        // &#125;)</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // jQuery 函数被调用后，返回值为一个对象</span></span><br><span class="line"><span class="xml">        function jQuery() &#123;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 对象在 javascript 中有几种方法可以定义</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // a) 字面量</span></span><br><span class="line"><span class="xml">            // return &#123;</span></span><br><span class="line"><span class="xml">            //     on: function () &#123;&#125;</span></span><br><span class="line"><span class="xml">            // &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // b) 通过构造函数来得到一对象</span></span><br><span class="line"><span class="xml">            function fn() &#123;&#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            return new fn();</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 为 jQuery 函数提供了一个别名</span></span><br><span class="line"><span class="xml">        var $ = jQuery;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        console.log($());</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">------------------------------------------------------</span></span><br><span class="line"><span class="xml">//以点击改变颜色的click为例</span></span><br><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>jQuery<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>大家好<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>大家好<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>大家好<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="comment">&lt;!-- &lt;script src="./libs/jquery-1.12.4.js"&gt;&lt;/script&gt; --&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"./libs/sizzle/sizzle.min.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">//2</span></span><br><span class="line"><span class="xml">        function jQuery(selector) &#123;</span></span><br><span class="line"><span class="xml">            return new jQuery.fn.init(selector);</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 设置别名，目的使用方便</span></span><br><span class="line"><span class="xml">        jQuery.fn = jQuery.prototype;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 3通过在原型上添加函数来充当构造函数</span></span><br><span class="line"><span class="xml">        jQuery.prototype.init = function (selector) &#123;</span></span><br><span class="line"><span class="xml">            // console.log(selector);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 是一个 DOM</span></span><br><span class="line"><span class="xml">            if(typeof selector == 'object') &#123;</span></span><br><span class="line"><span class="xml">                // 向 this 中添加DOM</span></span><br><span class="line"><span class="xml">                [].push.call(this, selector);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            &#125; else if(typeof selector == 'string') &#123;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">                // 在这里传入了一个选择器，通过这个选择器</span></span><br><span class="line"><span class="xml">                // 可以实现 DOM 元素的获取</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">                // 将获得的 DOM 添加至 this 上</span></span><br><span class="line"><span class="xml">                Sizzle(selector, document, this);</span></span><br><span class="line"><span class="xml">            &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            return this;</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 4设置别名，目的使用方便</span></span><br><span class="line"><span class="xml">        jQuery.prototype.init.prototype = jQuery.fn;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        jQuery.fn.css = function (attr, val) &#123;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 给谁设置样式？</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // console.log(this);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 将 this 这个伪数组变成真数组</span></span><br><span class="line"><span class="xml">            // 通过 Array.from 可以实现</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // nodes 是纯 DOM</span></span><br><span class="line"><span class="xml">            var nodes = Array.from(this);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            nodes.forEach(function (v) &#123;</span></span><br><span class="line"><span class="xml">                // console.log(v);</span></span><br><span class="line"><span class="xml">                v.style[attr] = val;</span></span><br><span class="line"><span class="xml">            &#125;)</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            return this;</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        jQuery.fn.on = function (type, cb) &#123;</span></span><br><span class="line"><span class="xml">            // console.log('on');</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // console.log(type);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // console.log(this);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 为谁添加事件？</span></span><br><span class="line"><span class="xml">            // 为获得到的 DOM 添加，DOM 被放到 this</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 将 this 这个伪数组变成真数组</span></span><br><span class="line"><span class="xml">            // 通过 Array.from 可以实现</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // nodes 是纯 DOM</span></span><br><span class="line"><span class="xml">            var nodes = Array.from(this);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // console.log(nodes);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            nodes.forEach(function (val) &#123;</span></span><br><span class="line"><span class="xml">                // console.log(val);</span></span><br><span class="line"><span class="xml">                val.addEventListener(type, function () &#123;</span></span><br><span class="line"><span class="xml">                    // console.log('你点击了');</span></span><br><span class="line"><span class="xml">                    // cb();</span></span><br><span class="line"><span class="xml">                    // 改变 回调函数的指向为 当前 DOM </span></span><br><span class="line"><span class="xml">                    cb.call(this);</span></span><br><span class="line"><span class="xml">                &#125;);</span></span><br><span class="line"><span class="xml">            &#125;)</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        jQuery.fn.attr = function () &#123;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">     //1</span></span><br><span class="line"><span class="xml">        var $ = jQuery;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // console.log($('h1'));</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // $('h1').css('color', 'red');</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        $('h1').on('click', function () &#123;</span></span><br><span class="line"><span class="xml">            // console.log(this);</span></span><br><span class="line"><span class="xml">            // console.log($(this));</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            $(this).css('color', 'red').css('fontSize', '18px');</span></span><br><span class="line"><span class="xml">        &#125;);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">//--------------------瀑布流布局扩展，其实现在waterfall.php中-----------------------------</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">// 所有的jQuery 插件其实都是在 jQuery 的原型上 添加的方法</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">jQuery.fn.waterfall = function () &#123;</span></span><br><span class="line"><span class="xml">    console.log('插件被调用了');</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // this 指的是谁？</span></span><br><span class="line"><span class="xml">    // this 指的是调用插件的 DOM</span></span><br><span class="line"><span class="xml">    // console.log(this);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 在 .items 中实现 .item 的瀑布流布局</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 获得所有需要定位子无素</span></span><br><span class="line"><span class="xml">    var items = $(this).children();</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 为 .items 设置为相对定位</span></span><br><span class="line"><span class="xml">    $(this).css(&#123;</span></span><br><span class="line"><span class="xml">        position: 'relative'</span></span><br><span class="line"><span class="xml">    &#125;);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 计算每列的宽度（可以根据所要显示的列数来确定）</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 假定 5 列</span></span><br><span class="line"><span class="xml">    var column = 5;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // .items 的宽度</span></span><br><span class="line"><span class="xml">    var cwidth = $(this).width();</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 每列的宽度</span></span><br><span class="line"><span class="xml">    var width = cwidth / column;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 将计算好的每列的宽度设置给所有的子元素</span></span><br><span class="line"><span class="xml">    items.width(width);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 定义一个数组，来记录每一列的高度</span></span><br><span class="line"><span class="xml">    var h = [];</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    // 需要为不同的子元素设置不同的定位坐标</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    items.each(function (key, val) &#123;</span></span><br><span class="line"><span class="xml">        // console.log(key);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 如果是第1行，top值 全部为 0 left 值为 索引值 * 定宽</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 根据列数，来判断第1的元素</span></span><br><span class="line"><span class="xml">        if(key <span class="tag">&lt; <span class="attr">column</span>) &#123;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 记录第1行元素的度度</span></span><br><span class="line"><span class="xml">            h.push($(this).height());</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            $(this).css(&#123;</span></span><br><span class="line"><span class="xml">                position: 'absolute',</span></span><br><span class="line"><span class="xml">                left: key * width,</span></span><br><span class="line"><span class="xml">                top: 0</span></span><br><span class="line"><span class="xml">            &#125;);</span></span><br><span class="line"><span class="xml">        &#125; else &#123; </span></span><br><span class="line"><span class="xml">            // 除了第 1 行外，top 值为最小的列的高度</span></span><br><span class="line"><span class="xml">            // left 值为 列的索引值 * 定宽</span></span><br><span class="line"><span class="xml">            </span></span><br><span class="line"><span class="xml">            // console.log(h)</span></span><br><span class="line"><span class="xml">            var min_val = h[0];</span></span><br><span class="line"><span class="xml">            var min_key = 0;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            for(var i=0; i<span class="tag">&lt;<span class="name">h.length;</span> <span class="attr">i</span>++) &#123;   //找最小的列高</span></span></span><br><span class="line"><span class="xml">                if(h[i] <span class="tag">&lt; <span class="attr">min_val</span>) &#123;</span></span></span><br><span class="line"><span class="xml">                    min_val = h[i];</span></span><br><span class="line"><span class="xml">                    min_key = i;</span></span><br><span class="line"><span class="xml">                &#125;</span></span><br><span class="line"><span class="xml">            &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            // 更新最小列</span></span><br><span class="line"><span class="xml">            h[min_key] += $(this).height();   //this指的是当前要放置的div块</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">            $(this).css(&#123;</span></span><br><span class="line"><span class="xml">                position: 'absolute',</span></span><br><span class="line"><span class="xml">                left: width * min_key,</span></span><br><span class="line"><span class="xml">                top: min_val</span></span><br><span class="line"><span class="xml">            &#125;)</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml">    &#125;);</span></span><br><span class="line"><span class="xml">&#125;</span></span><br></pre></td></tr></table></figure>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <div id="comment-container">
        </div>
    </div>
</div>
    </div>
</div>

<footer class="footer">
    <ul class="list-inline text-center">
        
        

        
        <li>
            <a target="_blank" href="http://weibo.com/5216026690">
                            <span class="fa-stack fa-lg">
                                  <i class="iconfont icon-weibo"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank" href="https://www.facebook.com/853788089@qq.com">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-facebook"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank"  href="https://gitee.com/dh1992">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://www.baidu.com">baidu</a></span>
        <span>/</span>
        
        <span><a href="#">Duheng&#39;s Page</a></span>
        <span>/</span>
        
        <span><a href="#">It helps SEO</a></span>
        <span>/</span>
        
    </p>
    
    
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<script src="/js/gitment.js"></script>
<script>
    var gitment = new Gitment({
        id: '封装一个简易的jquery',
        owner: 'aircloud',
        repo: 'hexo-aircloud-blog',
        oauth: {
            client_id: 'your-id-created-by-https://github.com/settings/applications/new',
            client_secret: 'your-secret-created-by-https://github.com/settings/applications/new',
        },
    })
    gitment.render('comment-container')
</script>

</html>
